/***
 * File			: TimeSpan.java
 * Subject		: Represents a time span.
 * Author		: Isaac Parenteau locus2k@gmail.com
 * Date			: May 4th, 2011
 * Revised		: October 18th, 2011
 */
package opengameflow.framework.time;

import opengameflow.framework.interfaces.IComparable;
import opengameflow.framework.interfaces.IFormattable;

public class TimeSpan implements IComparable<TimeSpan>, IFormattable {
	
	public final static String TAG = TimeSpan.class.getCanonicalName();
	
	public final static long MillisecondsInDays = 8640000L;
	public final static long MillisecondsInMins = 60000L;
	public final static long MillisecondsInHrs = 3600000L;
	public final static long MillisecondsInSec = 1000L;
	
	private long milliseconds;
	private long startTime;
	
	/**
	 * Default constructor. Sets everything to 0. 
	 */
	public TimeSpan() {
		this(0, 0, 0, 0, 0);
		startTime = System.currentTimeMillis();
	}
	
	/**
	 * 
	 * @param ticks
	 */
	public TimeSpan(long milliseconds){
		this.milliseconds = milliseconds;
	}
	
	/**
	 * 
	 * @param hours
	 * @param minutes
	 * @param seconds
	 */
	public TimeSpan(int hours, int minutes, int seconds){
		this(0, hours, minutes, seconds, 0);
	}
	
	/***
	 * 
	 * @param days
	 * @param hours
	 * @param minutes
	 * @param seconds
	 */
	public TimeSpan(int days, int hours, int minutes, int seconds){
		this(days, hours, minutes, seconds, 0);
	}
	
	/**
	 * 
	 * @param days
	 * @param hours
	 * @param minutes
	 * @param seconds
	 * @param milliseconds
	 */
	public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds){
		this(days * MillisecondsInDays + hours * MillisecondsInHrs + 
				minutes * MillisecondsInMins + seconds * MillisecondsInSec +
				milliseconds);
	}
	
	
	/**
	 * 
	 * @return
	 */
	public long getDays(){ return milliseconds / MillisecondsInDays; }
	
	/**
	 * 
	 * @return
	 */
	public long getHours() { return milliseconds / MillisecondsInHrs; }
	
	/**
	 * 
	 * @return
	 */
	public long getMilliseconds() { return milliseconds; }
	
	/**
	 * 
	 * @return
	 */
	public long getSeconds() { return milliseconds / MillisecondsInSec; }
	
	/**
	 * 
	 * @return
	 */
	public long getMinutes() { return milliseconds / MillisecondsInMins; }
	
	/**
	 * 
	 * @return
	 */
	public double getElapsedDays() { return getElapsedMilliseconds() / MillisecondsInDays; }
	
	/**
	 * 
	 * @return
	 */
	public double getElapsedHours() { return getElapsedMilliseconds() / MillisecondsInHrs; }
	
	/**
	 * 
	 * @return
	 */
	public double getElapsedMinutes() { return getElapsedMilliseconds() / MillisecondsInMins; }
	
	/**
	 * 
	 * @return
	 */
	public double getElapsedMilliseconds() { return System.currentTimeMillis() - startTime; }
	
	/**
	 * 
	 * @return
	 */
	public double getElapsedSeconds() { return getElapsedMilliseconds() / MillisecondsInSec; }
	
	
	@Override
	public String toString() {
		return "[TimeSpan] Elapsed Time (ms) = " + getElapsedMilliseconds();
	}
	
	@Override
	public String toString(String format, StringBuilder builder) {
		return format;
	}

	public boolean equals(TimeSpan other) {
		boolean retVal = this.milliseconds == other.getMilliseconds();
		return retVal;
	}
	

	@Override
	public int compareTo(TimeSpan obj) {
		return ((Long)this.milliseconds).compareTo(obj.getMilliseconds());
	}
}
